package org.seanye.erp.view;

import java.util.List;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.seanye.erp.view.timesheet.DateModel;

public class TimeSheetView extends ViewPart {

	public static final String ID = "org.seanye.erp.view.timesheet";

	private TreeViewer viewer;

	public TimeSheetView() {
	}

	@Override
	public void createPartControl(Composite parent) {
		GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent);

		viewer = new TreeViewer(parent);
		viewer.getTree().setHeaderVisible(true);
		viewer.getTree().setLinesVisible(true);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(
				viewer.getTree());

		viewer.setContentProvider(new ITreeContentProvider() {

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {

			}

			public void dispose() {

			}

			public Object[] getElements(Object inputElement) {
				if (inputElement instanceof List) {
					return ((List) inputElement).toArray(new Object[0]);
				}
				return null;
			}

			public boolean hasChildren(Object element) {
				if (element instanceof DateModel) {
					return ((DateModel) element).getChildren() != null
							&& !((DateModel) element).getChildren().isEmpty();
				}
				return false;
			}

			public Object getParent(Object element) {
				return null;
			}

			public Object[] getChildren(Object parentElement) {
				if (parentElement instanceof DateModel) {
					return ((DateModel) parentElement).getChildren().toArray(
							new Object[0]);
				}
				return null;
			}
		});

		viewer.setLabelProvider(new ITableLabelProvider() {

			public void removeListener(ILabelProviderListener listener) {

			}

			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			public void dispose() {

			}

			public void addListener(ILabelProviderListener listener) {

			}

			public String getColumnText(Object element, int columnIndex) {
				DateModel dm = (DateModel) element;
				return dm.toString();
			}

			public Image getColumnImage(Object element, int columnIndex) {
				return null;
			}
		});

		getSite().setSelectionProvider(viewer);
		viewer.setInput(DateModel.recentYears(2008, 4));
	}

	@Override
	public void setFocus() {
		viewer.getTree().setFocus();
	}

}
